package de.dlrg.wms.gui.forms;

import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import wms2010.interfaces.devices.IDevice;
import de.dlrg.wms.controller.controllers.ConnectivityDeviceController;
import de.dlrg.wms.gui.forms.generic.GenericForm;
import de.dlrg.wms.interfaces.controller.IDeviceController;
import de.dlrg.wms.interfaces.controller.IDeviceControllerCallback;

public class AssignStarter extends GenericForm implements IDeviceControllerCallback {
	private ConnectivityDeviceController connectivityDeviceController;
	private static int START_DISCOVERY_INDEX = 0;
	private Combo connectivities;
	private final Combo devices;
	private HashMap<Integer, IDeviceController> indexIDeviceControllerMapping = new HashMap<Integer, IDeviceController>();
	private HashMap<Integer, IDevice> deviceMapping = new HashMap<Integer, IDevice>();

	private int getNextIndex(Combo combo) {
		if (this.connectivities.getItemCount() > 0) {
			return combo.getItemCount();
		}
		return 0;
	}

	private void addDeviceControllerToMap(IDeviceController iDeviceController) {
		int i = this.getNextIndex(this.connectivities);
		this.indexIDeviceControllerMapping.put(i, iDeviceController);
		this.connectivities.add(iDeviceController.getName(), i);
	}

	private void addDeviceToMap(IDevice iDevice) {
		int i = this.getNextIndex(devices);
		this.deviceMapping.put(i, iDevice);
		this.devices.add(iDevice.getName(), i);
	}

	private IDevice getSelectedDevice() {
		return this.deviceMapping.get(this.devices.getSelectionIndex());
	}

	private IDeviceController getSelectedController() {
		return indexIDeviceControllerMapping.get(connectivities
				.getSelectionIndex());
	}

	public AssignStarter(Composite arg0, int arg1, final ConnectivityDeviceController connectivityDeviceController) {
		super(arg0, arg1);
		this.connectivityDeviceController = connectivityDeviceController;
		setLayout(new GridLayout(4, false));
						
								connectivities = new Combo(this, SWT.NONE);
								connectivities.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
										false, 1, 1));
				
						devices = new Combo(this, SWT.NONE);
						devices.addSelectionListener(new SelectionAdapter() {
							@Override
							public void widgetSelected(SelectionEvent e) {
								if (devices.getSelectionIndex() == START_DISCOVERY_INDEX) {
									getSelectedController().startDiscovery();
								}
							}
						});
						devices.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
								1, 1));
				new Label(this, SWT.NONE);
		
				Button Pair = new Button(this, SWT.NONE);
				Pair.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent arg0) {
						getSelectedDevice().rumble();
					}
				});
				Pair.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1,
						1));
				Pair.setText("Pair");
		new Label(this, SWT.NONE);
		new Label(this, SWT.NONE);
		new Label(this, SWT.NONE);
		new Label(this, SWT.NONE);

		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayout(new FormLayout());
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, false,
				false, 4, 1));

		this.initData();
	}

	@Override
	protected void initData() {
		this.clearDeviceList();

		for (IDeviceController iDeviceController : this.connectivityDeviceController
				.readAll()) {
			this.addDeviceControllerToMap(iDeviceController);
			iDeviceController.registerDeviceControllerCallback(this);
		}
	}
	
	private void clearDeviceList() {
		this.devices.removeAll();
		this.devices.add("Start Discovery", 0);
	}

	public void finishedDiscovery() {
		this.clearDeviceList();
		for (IDevice device : this.getSelectedController().getAllDevices()) {
			this.addDeviceToMap(device);
		}
	}

	@Override
	protected void okClicked() {
		// TODO Auto-generated method stub
		
	}
}
